﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace taurus.client
{


    /// <summary>
    /// 
    /// </summary>
    public class TObject : ITObject
    {
        private Dictionary<string, TDataWrapper> dataHolder = new Dictionary<string, TDataWrapper>();

        public bool containsKey(string key)
        {
            return dataHolder.ContainsKey(key);
        }

        private ICollection GetArray(string key)
        {
            return getValue<ICollection>(key);
        }

        public bool getBool(string key)
        {
            return getValue<bool>(key);
        }

        public virtual byte getByte(string key)
        {
            return getValue<byte>(key);
        }

        public byte[] getByteArray(string key)
        {
            return getValue<byte[]>(key);
        }

        public TDataWrapper get(string key)
        {
            return dataHolder[key];
        }

        public double getDouble(string key)
        {
            return getValue<double>(key);
        }

        public virtual float getFloat(string key)
        {
            return getValue<float>(key);
        }

        public int getInt(string key)
        {
            return getValue<int>(key);
        }

        public string[] getKeys()
        {
            string[] array = new string[dataHolder.Keys.Count];
            dataHolder.Keys.CopyTo(array, 0);
            return array;
        }

        public long getLong(string key)
        {
            return getValue<long>(key);
        }


        public ITArray getTArray(string key)
        {
            return getValue<ITArray>(key);
        }

        public ITObject getTObject(string key)
        {
            return getValue<ITObject>(key);
        }

        public virtual short getShort(string key)
        {
            return getValue<short>(key);
        }

        public string getString(string key)
        {
            return getValue<string>(key);
        }


        private T getValue<T>(string key)
        {
            if (!dataHolder.ContainsKey(key))
            {
                return default(T);
            }
            return (T)dataHolder[key].getObject();
        }

        public bool isNull(string key)
        {
            return (!containsKey(key) || (get(key).getObject() == null));
        }

        public static TObject newFromBinaryData(ByteArray ba)
        {
            return TDataSerializer.me().binary2Object(ba) as TObject;
        }

        public static TObject newFromJson(string json)
        {
            return TDataSerializer.me().json2object(json) as TObject;
        }

        public static TObject newInstance()
        {
            return new TObject();
        }

        public void put(string key, TDataWrapper val)
        {
            dataHolder[key] = val;
        }

        public void putBool(string key, bool val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.BOOL, val);
        }


        public void putByte(string key, byte val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.BYTE, val);
        }

        public void putByteArray(string key, byte[] val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.BYTE_ARRAY, val);
        }


        public void putDouble(string key, double val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.DOUBLE, val);
        }

        public void putFloat(string key, float val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.FLOAT, val);
        }

        public void putInt(string key, int val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.INT, val);
        }

        public void putLong(string key, long val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.LONG, val);
        }

        public void putNull(string key)
        {
            dataHolder[key] = new TDataWrapper(TDataType.NULL, null);
        }

        public void putTArray(string key, ITArray val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.TARRAY, val);
        }

        public void putTObject(string key, ITObject val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.TOBJECT, val);
        }

        public void putShort(string key, short val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.SHORT, val);
        }

        public void putString(string key, string val)
        {
            dataHolder[key] = new TDataWrapper(TDataType.STRING, val);
        }

        public void del(string key)
        {
            dataHolder.Remove(key);
        }

        public int size()
        {
            return dataHolder.Count;
        }

        public ByteArray toBinary()
        {
            return TDataSerializer.me().object2Binary(this);
        }

        public string toJson()
        {
            return TDataSerializer.me().object2json(this);
        }

        public string getUtfString(string key)
        {
            return getString(key);
        }

        public void putUtfString(string key, string val)
        {
            putString(key, val);
        }
    }

    /// <summary>
    /// TObjectLite
    /// </summary>
    public class TObjectLite : TObject
    {
        public TObjectLite() : base()
        {

        }

        public override byte getByte(string key)
        {
            int num = base.getInt(key);
            bool flag = num < 0;
            if (flag)
            {
                num = 256 + num;
            }
            return Convert.ToByte(num);
        }

        public override short getShort(string key)
        {
            int @int = base.getInt(key);
            return Convert.ToInt16(@int);
        }


        public override float getFloat(string key)
        {
            double @double = base.getDouble(key);
            return Convert.ToSingle(@double);
        }

    }
}

